<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller {
	public function login(){		
		$this->display();
		
	}
	public function code(){
		
	$config =    array(
    'fontSize'    =>    16,    // 验证码字体大小
    'length'      =>    4,     // 验证码位数
    'useNoise'    =>    false, // 关闭验证码杂点
	'imageH' =>$_GET['h'],
	'imageW' =>180,
);
			$Verify = new \Think\Verify($config);
			$Verify->codeSet = '0123456789'; 
$Verify->entry();
		}
	public function doLogin(){
		 
		//接收值	
		
		//判断用户在数据库中是否存在	
		//存在 允许登录	
		//不存在 显示错误信息
		$username=$_POST['username'];
		$password=$_POST['password'];
		$code=$_POST['code'];
		if(check_code($code) === false){       
			$this->error('验证码不正确');
       }
	  
	   $user=M('User');
	   $where['username']=$username;
	   $arr=$user->field('id,password,status,limit')->where($where)->find();
	   //是否被冻结
	   if($arr['status']==0){
	   //用户名正确
		   if($arr){
			  
				
				if($password==$arr['password']){//密码正确
					if($arr['limit']!=1){//判断权限
					$_SESSION['username']=$username;
				    $_SESSION['uid']=$arr['id'];
						$this->success('用户登录成功！',U('Index/index'));
					}else{
						$_SESSION['username']=$username;
				    $_SESSION['uid']=$arr['id'];
						$this->success('管理员登录成功！',U('Admin/adminindex'));
					}
				}else{
					$this->error('密码输入错误！');
				}
				
		   }else{
				$this->error('该用户不存在！');
		   }   	
		
		}else{
			$this->error('该用户已被冻结！');
		}
	}
	//退出
		public function delsession(){
		//1开启session
					session_start();
					  
					//2、清空session信息
					$_SESSION = array();
					  
					//3、清楚客户端sessionid
					if(isset($_COOKIE[session_name()]))
					{
					  setCookie(session_name(),'',time()-3600,'/');
					}
					//4、彻底销毁session
					session_destroy();
					$this->success('成功退出！',U('Index/index'));

	}
}
?>